package ReflectClassDemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo {
    //反射学生对象并实例化
    public static  void reflectNewInstance() {
        Class<?> c3 = null;
        try{
            c3 = Class.forName("ReflectClassDemo.Student");
            Student student = (Student) c3.newInstance();
            System.out.println(student);
            student.sleep();
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }catch(InstantiationException e){
            e.printStackTrace();
        }catch(IllegalAccessException e){
            e.printStackTrace();
        }
    }
    // 反射私有的构造方法 屏蔽内容为获得公有的构造方法
    public static void reflectPrivateConstructor() {
        Class<?> c3 = null;
        try{
            c3 = Class.forName("ReflectClassDemo.Student");
            Constructor<?> constructor =
                    c3.getDeclaredConstructor(String.class,int.class);
            //私有属性和方法需要设置可在类外调用和修改的权限(否则会抛异常)
            constructor.setAccessible(true);
            Student student = (Student) constructor.newInstance("hong",19);
            System.out.println(student);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    // 反射私有属性
    public static void reflectPrivateField() {
        Class<?> c3 = null;
        try{
            c3 = Class.forName("ReflectClassDemo.Student");
            Student student = (Student) c3.newInstance();
            Field field = c3.getDeclaredField("name");
            field.setAccessible(true);
            field.set(student,"zhugeliang");
            System.out.println(student);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
    // 反射私有方法
    public static void reflectPrivateMethod() {
        Class<?> c3 = null;
        try{
            c3 = Class.forName("ReflectClassDemo.Student");
            Student student = (Student) c3.newInstance();
            Method method = c3.getDeclaredMethod("function", String.class);
            method.setAccessible(true);
            method.invoke(student,"传一个参数");
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        reflectNewInstance();
        reflectPrivateConstructor();
        reflectPrivateField();
        reflectPrivateMethod();
    }

}
